package com.wafersystems.springbootdemo.config;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 定时任务AOP
 * @author tandk
 * @date 2018/12/5 15:09
 */
@Slf4j
@Aspect
@Component
public class ScheduledRedisAspect {

    /**
     * 定时任务执行前执行
     * @param joinPoint
     * @param scheduled
     */
    @Before("execution(public * com.wafersystems.springbootdemo.scheduled.*.*(..)) && @annotation(scheduled)")
    public void before(JoinPoint joinPoint, Scheduled scheduled) {
        long startTime = System.currentTimeMillis();
        log.info("[定时任务][{}][{}]执行before，执行时间[{}]", joinPoint.getTarget().getClass().getSimpleName(), joinPoint.getSignature().getName(), System.currentTimeMillis()-startTime);
    }

    /**
     * 定时任务执行后执行
     * @param joinPoint
     * @param scheduled
     */
    @After("execution(public * com.wafersystems.springbootdemo.scheduled.*.*(..)) && @annotation(scheduled)")
    public void after(JoinPoint joinPoint, Scheduled scheduled) {
        long startTime = System.currentTimeMillis();
        log.info("[定时任务][{}][{}]执行after，执行时间[{}]", joinPoint.getTarget().getClass().getSimpleName(), joinPoint.getSignature().getName(), System.currentTimeMillis()-startTime);
    }

}